package com.rc.activity.sync.adapter;

import android.accounts.Account;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.rc.activity.ReceiptBoxDetailAddItem;
import com.rc.activity.dao.tbl_UserDao;
import com.rc.activity.provider.ContentProviderApp;
import com.rc.activity.sync.util.ItemSync;
import com.rc.activity.sync.util.MerchantSync;
import com.rc.activity.sync.util.ReceiptSync;
import com.rc.activity.sync.util.ReportSync;
import com.rc.activity.sync.util.TripItemsSync;
import com.rc.activity.sync.util.TripSync;
import com.rc.fragment.TripFragment;
import com.rc.util.Const;
import com.rc.util.SharedObjects;

public class RICSyncAdapter extends AbstractThreadedSyncAdapter {

	private Context ct;
	private Cursor cursor = null;
	private tbl_UserDao daoUser;
	private static MerchantSync merchantSync;
	public static ReceiptSync receiptSync;
	public static ReportSync reportSync;
	public static TripSync tripSync;
	public static ItemSync itemSync;
	public static TripItemsSync tripItemSync;

	public RICSyncAdapter(Context context, boolean autoInitialize) {
		super(context, autoInitialize);
		ct = context;
		daoUser = ContentProviderApp.daoSession.getTbl_UserDao();
		// Handler sync

	}

	@Override
	public void onPerformSync(Account account, Bundle extras, String authority,
			ContentProviderClient provider, SyncResult syncResult) {
		Log.e("autoSync", 1 + "");

		if ("".equals(Const.AUTHENKEY)) {
			cursor = ContentProviderApp.db.query(daoUser.getTablename(),
					daoUser.getAllColumns(), null, null, null, null, null);
			while (cursor.moveToNext()) {
				Const.AUTHENKEY = cursor
						.getString(cursor
								.getColumnIndex(tbl_UserDao.Properties.Token.columnName));
			}
			cursor.close();
		}

		new syncMerchant().execute("");

	}

	private class syncMerchant extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Syn Merchant
			merchantSync = new MerchantSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			merchantSync.updateMerchant();

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			new syncReceipt().execute("");

		}

	}

	private class syncReceipt extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Syn Merchant
			receiptSync = new ReceiptSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			new syncReport().execute("");
		}
	}

	private class syncReport extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Sync report
			reportSync = new ReportSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			reportSync.deleteReport();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			new syncTrip().execute("");
		}

	}

	private class syncTrip extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Sync Trip
			tripSync = new TripSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			tripSync.deleteTrip();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			new syncItem().execute("");
		}

	}

	private class syncItem extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Sync Item
			itemSync = new ItemSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			new syncTripItems().execute("");
		}

	}

	private class syncTripItems extends AsyncTask<String, Void, Void> {

		protected Void doInBackground(String... urls) {
			// Syn Merchant
			// Sync TripItem
			tripItemSync = new TripItemsSync(ct, ContentProviderApp.db,
					ContentProviderApp.daoSession);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
		}

	}

}
